import ItemBaseData from "../com/data/ItemBaseData";

/**author:yebinx email:yebinx@21cm.com*/
export default class GameCfgData{
    private static _ins:GameCfgData;
    public data:any;
    private gateData:any;
    private filterAppid:string="wx35fef987e3ae9dd8";
    private mIsLoading:boolean=false;
    constructor(){

    }

    public init(_data){
        this.data=_data;
    }

    public loadGateCfgDate(currGate:number,complete:Function=null,pross:Function=null){
        let tindex = Math.ceil(currGate/100);
        let turl="res/gates/gate_"+tindex+".json";
        this.mIsLoading=true;
        Laya.loader.load(turl, Laya.Handler.create(this, (_data)=>{
            this.gateData=_data;
            if(complete)complete();
            this.mIsLoading=false;
        }),  Laya.Handler.create(this, (res)=>{if(pross)pross(res)}), Laya.Loader.JSON);
        
    }


   public getGames(){
        let tgames=this.data.games;
        for(let i=0;i<tgames.length;i++){
            if(tgames[i].appId==this.filterAppid){
               tgames.splice(i,1);
                break;
            }
        }
        return tgames;
   }

   public getItemsByType(_type:number){
       let tarr=[];
        let ttems=this.data["items"];
        let tdata:ItemBaseData;
        for(let key in ttems){
            tdata=new ItemBaseData();
            tdata.copy(ttems[key]);
            if(tdata.type==_type)tarr.push(tdata);
        }
        return tarr;
   }

   public getEditItems(){
       let ttararr = [];
       let tarr=[1,2,3,4,5];
       let tfilters=[];
       let ttems=this.data["items"];
       let tdata:ItemBaseData;
       for(let key in ttems){
            tdata=new ItemBaseData();
            tdata.copy(ttems[key]);
            if(tarr.indexOf(tdata.id)>=0)ttararr.push(tdata);
            else if(tdata.type==2 || tdata.type==3 || tdata.type==4){
                if(tfilters.indexOf(tdata.id)<0)ttararr.push(tdata);
            }
       }
       return ttararr;
   }

   public getItem(_id:number){
        return this.data.items[_id+""];
   }

   public getGateCfg(_id:number){
        return this.gateData[_id];
   }

   public getItemIcon(_id:number){
        return this.data.items[_id+""].icon;
   }

   public getNotice(){
        let tdata = this.data.notice;
        return tdata;
   }

   public get isLoading(){
        return this.mIsLoading;
   }

    public static getIns():GameCfgData{
        if(!this._ins)this._ins=new GameCfgData();
        return this._ins;
    }
}